home *** CD-ROM | disk | FTP | other *** search
/ SGI Freeware 1999 August / SGI Freeware 1999 August.iso / dist / fw_perl.idb / usr / freeware / catman / u_man / cat1 / perlfaq9.Z / perlfaq9
Encoding:
Text File  |  1998-10-28  |  31.8 KB  |  859 lines

  1.  
  2.  
  3.  
  4.      PPPPEEEERRRRLLLLFFFFAAAAQQQQ9999((((1111))))     22223333////JJJJuuuullll////99998888 ((((ppppeeeerrrrllll 5555....000000005555,,,, ppppaaaattttcccchhhh 00002222))))       PPPPEEEERRRRLLLLFFFFAAAAQQQQ9999((((1111))))
  5.  
  6.  
  7.  
  8.      NNNNAAAAMMMMEEEE
  9.       perlfaq9 - Networking    ($Revision: 1.20 $, $Date: 1998/06/22
  10.       18:31:09 $)
  11.  
  12.      DDDDEEEESSSSCCCCRRRRIIIIPPPPTTTTIIIIOOOONNNN
  13.       This section deals with questions related to networking, the
  14.       internet, and    a few on the web.
  15.  
  16.       MMMMyyyy CCCCGGGGIIII ssssccccrrrriiiipppptttt    rrrruuuunnnnssss ffffrrrroooommmm tttthhhheeee ccccoooommmmmmmmaaaannnndddd lllliiiinnnneeee bbbbuuuutttt nnnnooootttt tttthhhheeee
  17.       bbbbrrrroooowwwwsssseeeerrrr....   ((((555500000000 SSSSeeeerrrrvvvveeeerrrr EEEErrrrrrrroooorrrr))))
  18.  
  19.       If you can demonstrate that you've read the following    FAQs
  20.       and that your    problem    isn't something    simple that can    be
  21.       easily answered, you'll probably receive a courteous and
  22.       useful reply to your question    if you post it on
  23.       comp.infosystems.www.authoring.cgi (if it's something    to do
  24.       with HTTP, HTML, or the CGI protocols).  Questions that
  25.       appear to be Perl questions but are really CGI ones that are
  26.       posted to comp.lang.perl.misc    may not    be so well received.
  27.  
  28.       The useful FAQs and related documents    are:
  29.  
  30.           CGI FAQ
  31.           http://www.webthing.com/page.cgi/cgifaq
  32.  
  33.           Web FAQ
  34.           http://www.boutell.com/faq/
  35.  
  36.           WWW Security FAQ
  37.           http://www.w3.org/Security/Faq/
  38.  
  39.           HTTP Spec
  40.           http://www.w3.org/pub/WWW/Protocols/HTTP/
  41.  
  42.           HTML Spec
  43.           http://www.w3.org/TR/REC-html40/
  44.           http://www.w3.org/pub/WWW/MarkUp/
  45.  
  46.           CGI Spec
  47.           http://www.w3.org/CGI/
  48.  
  49.           CGI Security FAQ
  50.           http://www.go2net.com/people/paulp/cgi-security/safe-cgi.txt
  51.  
  52.  
  53.       HHHHoooowwww ccccaaaannnn IIII ggggeeeetttt    bbbbeeeetttttttteeeerrrr eeeerrrrrrrroooorrrr mmmmeeeessssssssaaaaggggeeeessss ffffrrrroooommmm aaaa CCCCGGGGIIII pppprrrrooooggggrrrraaaammmm????
  54.  
  55.       Use the CGI::Carp module.  It    replaces warn and die, plus
  56.       the normal Carp modules carp,    croak, and confess functions
  57.       with more verbose and    safer versions.     It still sends    them
  58.       to the normal    server error log.
  59.  
  60.  
  61.  
  62.  
  63.      Page 1                        (printed 10/23/98)
  64.  
  65.  
  66.  
  67.  
  68.  
  69.  
  70.      PPPPEEEERRRRLLLLFFFFAAAAQQQQ9999((((1111))))     22223333////JJJJuuuullll////99998888 ((((ppppeeeerrrrllll 5555....000000005555,,,, ppppaaaattttcccchhhh 00002222))))       PPPPEEEERRRRLLLLFFFFAAAAQQQQ9999((((1111))))
  71.  
  72.  
  73.  
  74.           use CGI::Carp;
  75.           warn "This is a complaint";
  76.           die "But this one    is serious";
  77.  
  78.       The following    use of CGI::Carp also redirects    errors to a
  79.       file of your choice, placed in a BEGIN block to catch
  80.       compile-time warnings    as well:
  81.  
  82.           BEGIN {
  83.           use CGI::Carp    qw(carpout);
  84.           open(LOG, ">>/var/local/cgi-logs/mycgi-log")
  85.               or die "Unable to    append to mycgi-log: $!\n";
  86.           carpout(*LOG);
  87.           }
  88.  
  89.       You can even arrange for fatal errors    to go back to the
  90.       client browser, which    is nice    for your own debugging,    but
  91.       might    confuse    the end    user.
  92.  
  93.           use CGI::Carp qw(fatalsToBrowser);
  94.           die "Bad error here";
  95.  
  96.       Even if the error happens before you get the HTTP header
  97.       out, the module will try to take care    of this    to avoid the
  98.       dreaded server 500 errors.  Normal warnings still go out to
  99.       the server error log (or wherever you've sent    them with
  100.       carpout) with    the application    name and date stamp prepended.
  101.  
  102.       HHHHoooowwww ddddoooo IIII rrrreeeemmmmoooovvvveeee HHHHTTTTMMMMLLLL ffffrrrroooommmm aaaa ssssttttrrrriiiinnnngggg????
  103.  
  104.       The most correct way (albeit not the fastest)    is to use
  105.       HTML::Parse from CPAN    (part of the libwww-perl distribution,
  106.       which    is a must-have module for all web hackers).
  107.  
  108.       Many folks attempt a simple-minded regular expression
  109.       approach, like s/<.*?>//g, but that fails in many cases
  110.       because the tags may continue    over line breaks, they may
  111.       contain quoted angle-brackets, or HTML comment may be
  112.       present.  Plus folks forget to convert entities, like    <
  113.       for example.
  114.  
  115.       Here's one "simple-minded" approach, that works for most
  116.       files:
  117.  
  118.           #!/usr/bin/perl -p0777
  119.           s/<(?:[^>'"]*|(['"]).*?\1)*>//gs
  120.  
  121.       If you want a    more complete solution,    see the    3-stage
  122.       striphtml program in
  123.       http://www.perl.com/CPAN/authors/Tom_Christiansen/scripts/striphtml.gz
  124.       .
  125.  
  126.  
  127.  
  128.  
  129.      Page 2                        (printed 10/23/98)
  130.  
  131.  
  132.  
  133.  
  134.  
  135.  
  136.      PPPPEEEERRRRLLLLFFFFAAAAQQQQ9999((((1111))))     22223333////JJJJuuuullll////99998888 ((((ppppeeeerrrrllll 5555....000000005555,,,, ppppaaaattttcccchhhh 00002222))))       PPPPEEEERRRRLLLLFFFFAAAAQQQQ9999((((1111))))
  137.  
  138.  
  139.  
  140.       Here are some    tricky cases that you should think about when
  141.       picking a solution:
  142.  
  143.           <IMG SRC = "foo.gif" ALT = "A > B">
  144.  
  145.           <IMG SRC = "foo.gif"
  146.            ALT = "A > B">
  147.  
  148.           <!-- <A comment> -->
  149.  
  150.           <script>if (a<b && a>c)</script>
  151.  
  152.           <# Just data #>
  153.  
  154.           <![INCLUDE CDATA [ >>>>>>>>>>>> ]]>
  155.  
  156.       If HTML comments include other tags, those solutions would
  157.       also break on    text like this:
  158.  
  159.           <!-- This    section    commented out.
  160.           <B>You can't see me!</B>
  161.           -->
  162.  
  163.  
  164.       HHHHoooowwww ddddoooo IIII eeeexxxxttttrrrraaaacccctttt UUUURRRRLLLLssss????
  165.  
  166.       A quick but imperfect    approach is
  167.  
  168.           #!/usr/bin/perl -n00
  169.           #    qxurl -    tchrist@perl.com
  170.           print "$2\n" while m{
  171.           < \s*
  172.             A \s+ HREF \s* = \s* (["'])    (.*?) \1
  173.           \s* >
  174.           }gsix;
  175.  
  176.       This version does not    adjust relative    URLs, understand
  177.       alternate bases, deal    with HTML comments, deal with HREF and
  178.       NAME attributes in the same tag, or accept URLs themselves
  179.       as arguments.     It also runs about 100x faster    than a more
  180.       "complete" solution using the    LWP suite of modules, such as
  181.       the
  182.       http://www.perl.com/CPAN/authors/Tom_Christiansen/scripts/xurl.gz
  183.       program.
  184.  
  185.       HHHHoooowwww ddddoooo IIII ddddoooowwwwnnnnllllooooaaaadddd aaaa ffffiiiilllleeee ffffrrrroooommmm    tttthhhheeee uuuusssseeeerrrr''''ssss mmmmaaaacccchhhhiiiinnnneeee????  HHHHoooowwww ddddoooo IIII
  186.       ooooppppeeeennnn aaaa ffffiiiilllleeee oooonnnn aaaannnnooootttthhhheeeerrrr mmmmaaaacccchhhhiiiinnnneeee????
  187.  
  188.       In the context of an HTML form, you can use what's known as
  189.       mmmmuuuullllttttiiiippppaaaarrrrtttt////ffffoooorrrrmmmm----ddddaaaattttaaaa encoding.     The CGI.pm module (available
  190.       from CPAN) supports this in the _s_t_a_r_t__m_u_l_t_i_p_a_r_t__f_o_r_m()
  191.       method, which    isn't the same as the _s_t_a_r_t_f_o_r_m() method.
  192.  
  193.  
  194.  
  195.      Page 3                        (printed 10/23/98)
  196.  
  197.  
  198.  
  199.  
  200.  
  201.  
  202.      PPPPEEEERRRRLLLLFFFFAAAAQQQQ9999((((1111))))     22223333////JJJJuuuullll////99998888 ((((ppppeeeerrrrllll 5555....000000005555,,,, ppppaaaattttcccchhhh 00002222))))       PPPPEEEERRRRLLLLFFFFAAAAQQQQ9999((((1111))))
  203.  
  204.  
  205.  
  206.       HHHHoooowwww ddddoooo IIII mmmmaaaakkkkeeee    aaaa ppppoooopppp----uuuupppp mmmmeeeennnnuuuu iiiinnnn HHHHTTTTMMMMLLLL????
  207.  
  208.       Use the <<<<SSSSEEEELLLLEEEECCCCTTTT>>>> and <<<<OOOOPPPPTTTTIIIIOOOONNNN>>>>    tags.  The CGI.pm module
  209.       (available from CPAN)    supports this widget, as well as many
  210.       others, including some that it cleverly synthesizes on its
  211.       own.
  212.  
  213.       HHHHoooowwww ddddoooo IIII ffffeeeettttcccchhhh aaaannnn HHHHTTTTMMMMLLLL ffffiiiilllleeee????
  214.  
  215.       One approach,    if you have the    lynx text-based    HTML browser
  216.       installed on your system, is this:
  217.  
  218.           $html_code = `lynx -source $url`;
  219.           $text_data = `lynx -dump $url`;
  220.  
  221.       The libwww-perl (LWP)    modules    from CPAN provide a more
  222.       powerful way to do this.  They work through proxies, and
  223.       don't    require    lynx:
  224.  
  225.           #    simplest version
  226.           use LWP::Simple;
  227.           $content = get($URL);
  228.  
  229.           #    or print HTML from a URL
  230.           use LWP::Simple;
  231.           getprint "http://www.sn.no/libwww-perl/";
  232.  
  233.           #    or print ASCII from HTML from a    URL
  234.           use LWP::Simple;
  235.           use HTML::Parse;
  236.           use HTML::FormatText;
  237.           my ($html, $ascii);
  238.           $html = get("http://www.perl.com/");
  239.           defined $html
  240.           or die "Can't    fetch HTML from    http://www.perl.com/";
  241.           $ascii = HTML::FormatText->new->format(parse_html($html));
  242.           print $ascii;
  243.  
  244.  
  245.       HHHHoooowwww ddddoooo IIII aaaauuuuttttoooommmmaaaatttteeee aaaannnn HHHHTTTTMMMMLLLL ffffoooorrrrmmmm ssssuuuubbbbmmmmiiiissssssssiiiioooonnnn????
  246.  
  247.       If you're submitting values using the    GET method, create a
  248.       URL and encode the form using    the query_form method:
  249.  
  250.           use LWP::Simple;
  251.           use URI::URL;
  252.  
  253.           my $url =    url('http://www.perl.com/cgi-bin/cpan_mod');
  254.           $url->query_form(module => 'DB_File', readme => 1);
  255.           $content = get($url);
  256.  
  257.       If you're using the POST method, create your own user    agent
  258.  
  259.  
  260.  
  261.      Page 4                        (printed 10/23/98)
  262.  
  263.  
  264.  
  265.  
  266.  
  267.  
  268.      PPPPEEEERRRRLLLLFFFFAAAAQQQQ9999((((1111))))     22223333////JJJJuuuullll////99998888 ((((ppppeeeerrrrllll 5555....000000005555,,,, ppppaaaattttcccchhhh 00002222))))       PPPPEEEERRRRLLLLFFFFAAAAQQQQ9999((((1111))))
  269.  
  270.  
  271.  
  272.       and encode the content appropriately.
  273.  
  274.           use HTTP::Request::Common    qw(POST);
  275.           use LWP::UserAgent;
  276.  
  277.           $ua = LWP::UserAgent->new();
  278.           my $req =    POST 'http://www.perl.com/cgi-bin/cpan_mod',
  279.                  [ module => 'DB_File', readme => 1    ];
  280.           $content = $ua->request($req)->as_string;
  281.  
  282.  
  283.       HHHHoooowwww ddddoooo IIII ddddeeeeccccooooddddeeee oooorrrr ccccrrrreeeeaaaatttteeee tttthhhhoooosssseeee %%%%----eeeennnnccccooooddddiiiinnnnggggssss oooonnnn tttthhhheeee wwwweeeebbbb????
  284.  
  285.       Here's an example of decoding:
  286.  
  287.           $string =    "http://altavista.digital.com/cgi-bin/query?pg=q&what=news&fmt=.&q=%2Bcgi-bin+%2Bperl.exe";
  288.           $string =~ s/%([a-fA-F0-9]{2})/chr(hex($1))/ge;
  289.  
  290.       Encoding is a    bit harder, because you    can't just blindly
  291.       change all the non-alphanumunder character (\W) into their
  292.       hex escapes.    It's important that characters with special
  293.       meaning like / and ?    _n_o_t be translated.  Probably the
  294.       easiest way to get this right    is to avoid reinventing    the
  295.       wheel    and just use the URI::Escape module, which is part of
  296.       the libwww-perl package (LWP)    available from CPAN.
  297.  
  298.       HHHHoooowwww ddddoooo IIII rrrreeeeddddiiiirrrreeeecccctttt ttttoooo aaaannnnooootttthhhheeeerrrr ppppaaaaggggeeee????
  299.  
  300.       Instead of sending back a Content-Type as the    headers    of
  301.       your reply, send back    a Location: header.  Officially    this
  302.       should be a URI: header, so the CGI.pm module    (available
  303.       from CPAN) sends back    both:
  304.  
  305.           Location:    http://www.domain.com/newpage
  306.           URI: http://www.domain.com/newpage
  307.  
  308.       Note that relative URLs in these headers can cause strange
  309.       effects because of "optimizations" that servers do.
  310.  
  311.           $url = "http://www.perl.com/CPAN/";
  312.           print "Location: $url\n\n";
  313.           exit;
  314.  
  315.       To be    correct    to the spec, each of those "\n"    should really
  316.       each be "\015\012", but unless you're    stuck on MacOS,    you
  317.       probably won't notice.
  318.  
  319.       HHHHoooowwww ddddoooo IIII ppppuuuutttt aaaa ppppaaaasssssssswwwwoooorrrrdddd oooonnnn mmmmyyyy    wwwweeeebbbb ppppaaaaggggeeeessss????
  320.  
  321.       That depends.     You'll    need to    read the documentation for
  322.       your web server, or perhaps check some of the    other FAQs
  323.       referenced above.
  324.  
  325.  
  326.  
  327.      Page 5                        (printed 10/23/98)
  328.  
  329.  
  330.  
  331.  
  332.  
  333.  
  334.      PPPPEEEERRRRLLLLFFFFAAAAQQQQ9999((((1111))))     22223333////JJJJuuuullll////99998888 ((((ppppeeeerrrrllll 5555....000000005555,,,, ppppaaaattttcccchhhh 00002222))))       PPPPEEEERRRRLLLLFFFFAAAAQQQQ9999((((1111))))
  335.  
  336.  
  337.  
  338.       HHHHoooowwww ddddoooo IIII eeeeddddiiiitttt    mmmmyyyy ....hhhhttttppppaaaasssssssswwwwdddd aaaannnndddd ....hhhhttttggggrrrroooouuuupppp ffffiiiilllleeeessss    wwwwiiiitttthhhh PPPPeeeerrrrllll????
  339.  
  340.       The HTTPD::UserAdmin and HTTPD::GroupAdmin modules provide a
  341.       consistent OO    interface to these files, regardless of    how
  342.       they're stored.  Databases may be text, dbm, Berkley DB or
  343.       any database with a DBI compatible driver.  HTTPD::UserAdmin
  344.       supports files used by the `Basic' and `Digest'
  345.       authentication schemes.  Here's an example:
  346.  
  347.           use HTTPD::UserAdmin ();
  348.           HTTPD::UserAdmin
  349.             ->new(DB =>    "/foo/.htpasswd")
  350.             ->add($username => $password);
  351.  
  352.  
  353.       HHHHoooowwww ddddoooo IIII mmmmaaaakkkkeeee    ssssuuuurrrreeee uuuusssseeeerrrrssss ccccaaaannnn''''tttt eeeennnntttteeeerrrr vvvvaaaalllluuuueeeessss iiiinnnnttttoooo aaaa ffffoooorrrrmmmm tttthhhhaaaatttt
  354.       ccccaaaauuuusssseeee    mmmmyyyy CCCCGGGGIIII ssssccccrrrriiiipppptttt ttttoooo ddddoooo bbbbaaaadddd    tttthhhhiiiinnnnggggssss????
  355.  
  356.       Read the CGI security    FAQ, at    http://www-
  357.       genome.wi.mit.edu/WWW/faqs/www-security-faq.html, and    the
  358.       Perl/CGI FAQ at http://www.perl.com/CPAN/doc/FAQs/cgi/perl-
  359.       cgi-faq.html.
  360.  
  361.       In brief: use    tainting (see the _p_e_r_l_s_e_c manpage), which
  362.       makes    sure that data from outside your script    (eg, CGI
  363.       parameters) are never    used in    eval or    system calls.  In
  364.       addition to tainting,    never use the single-argument form of
  365.       _s_y_s_t_e_m() or _e_x_e_c().  Instead,    supply the command and
  366.       arguments as a list, which prevents shell globbing.
  367.  
  368.       HHHHoooowwww ddddoooo IIII ppppaaaarrrrsssseeee aaaa mmmmaaaaiiiillll    hhhheeeeaaaaddddeeeerrrr????
  369.  
  370.       For a    quick-and-dirty    solution, try this solution derived
  371.       from page 222    of the 2nd edition of "Programming Perl":
  372.  
  373.           $/ = '';
  374.           $header =    <MSG>;
  375.           $header =~ s/\n\s+/ /g;       # merge continuation    lines
  376.           %head = (    UNIX_FROM_LINE,    split /^([-\w]+):\s*/m,    $header    );
  377.  
  378.       That solution    doesn't    do well    if, for    example, you're    trying
  379.       to maintain all the Received lines.  A more complete
  380.       approach is to use the Mail::Header module from CPAN (part
  381.       of the MailTools package).
  382.  
  383.       HHHHoooowwww ddddoooo IIII ddddeeeeccccooooddddeeee aaaa CCCCGGGGIIII    ffffoooorrrrmmmm????
  384.  
  385.       You use a standard module, probably CGI.pm.  Under no
  386.       circumstances    should you attempt to do so by hand!
  387.  
  388.       You'll see a lot of CGI programs that    blindly    read from
  389.       STDIN    the number of bytes equal to CONTENT_LENGTH for    POSTs,
  390.  
  391.  
  392.  
  393.      Page 6                        (printed 10/23/98)
  394.  
  395.  
  396.  
  397.  
  398.  
  399.  
  400.      PPPPEEEERRRRLLLLFFFFAAAAQQQQ9999((((1111))))     22223333////JJJJuuuullll////99998888 ((((ppppeeeerrrrllll 5555....000000005555,,,, ppppaaaattttcccchhhh 00002222))))       PPPPEEEERRRRLLLLFFFFAAAAQQQQ9999((((1111))))
  401.  
  402.  
  403.  
  404.       or grab QUERY_STRING for decoding GETs.  These programs are
  405.       very poorly written.    They only work sometimes.  They
  406.       typically forget to check the    return value of    the _r_e_a_d()
  407.       system call, which is    a cardinal sin.     They don't handle
  408.       HEAD requests.  They don't handle multipart forms used for
  409.       file uploads.     They don't deal with GET/POST combinations
  410.       where    query fields are in more than one place.  They don't
  411.       deal with keywords in    the query string.
  412.  
  413.       In short, they're bad    hacks.    Resist them at all costs.
  414.       Please do not    be tempted to reinvent the wheel.  Instead,
  415.       use the CGI.pm or CGI_Lite.pm    (available from    CPAN), or if
  416.       you're trapped in the    module-free land of perl1 .. perl4,
  417.       you might look into cgi-lib.pl (available from
  418.       http://www.bio.cam.ac.uk/web/form.html).
  419.  
  420.       Make sure you    know whether to    use a GET or a POST in your
  421.       form.     GETs should only be used for something    that doesn't
  422.       update the server.  Otherwise    you can    get mangled databases
  423.       and repeated feedback    mail messages.    The fancy word for
  424.       this is ``idempotency''.  This simply    means that there
  425.       should be no difference between making a GET request for a
  426.       particular URL once or multiple times.  This is because the
  427.       HTTP protocol    definition says    that a GET request may be
  428.       cached by the    browser, or server, or an intervening proxy.
  429.       POST requests    cannot be cached, because each request is
  430.       independent and matters.  Typically, POST requests change or
  431.       depend on state on the server    (query or update a database,
  432.       send mail, or    purchase a computer).
  433.  
  434.       HHHHoooowwww ddddoooo IIII cccchhhheeeecccckkkk aaaa vvvvaaaalllliiiidddd mmmmaaaaiiiillll aaaaddddddddrrrreeeessssssss????
  435.  
  436.       You can't, at    least, not in real time.  Bummer, eh?
  437.  
  438.       Without sending mail to the address and seeing whether
  439.       there's a human on the other hand to answer you, you cannot
  440.       determine whether a mail address is valid.  Even if you
  441.       apply    the mail header    standard, you can have problems,
  442.       because there    are deliverable    addresses that aren't RFC-822
  443.       (the mail header standard) compliant,    and addresses that
  444.       aren't deliverable which are compliant.
  445.  
  446.       Many are tempted to try to eliminate many frequently-invalid
  447.       mail addresses with a    simple regexp, such as /^[\w.-
  448.       ]+\@([\w.-]\.)+\w+$/.     It's a    very bad idea.    However, this
  449.       also throws out many valid ones, and says nothing about
  450.       potential deliverability, so is not suggested.  Instead, see
  451.       http://www.perl.com/CPAN/authors/Tom_Christiansen/scripts/ckaddr.gz
  452.       , which actually checks against the full RFC spec (except
  453.       for nested comments),    looks for addresses you    may not    wish
  454.       to accept mail to (say, Bill Clinton or your postmaster),
  455.       and then makes sure that the hostname    given can be looked up
  456.  
  457.  
  458.  
  459.      Page 7                        (printed 10/23/98)
  460.  
  461.  
  462.  
  463.  
  464.  
  465.  
  466.      PPPPEEEERRRRLLLLFFFFAAAAQQQQ9999((((1111))))     22223333////JJJJuuuullll////99998888 ((((ppppeeeerrrrllll 5555....000000005555,,,, ppppaaaattttcccchhhh 00002222))))       PPPPEEEERRRRLLLLFFFFAAAAQQQQ9999((((1111))))
  467.  
  468.  
  469.  
  470.       in the DNS MX    records.  It's not fast, but it    works for what
  471.       it tries to do.
  472.  
  473.       Our best advice for verifying    a person's mail    address    is to
  474.       have them enter their    address    twice, just as you normally do
  475.       to change a password.     This usually weeds out    typos.    If
  476.       both versions    match, send mail to that address with a
  477.       personal message that    looks somewhat like:
  478.  
  479.           Dear someuser@host.com,
  480.  
  481.           Please confirm the mail address you gave us Wed May  6 09:38:41
  482.           MDT 1998 by replying to this message.  Include the string
  483.           "Rumpelstiltskin"    in that    reply, but spelled in reverse; that is,
  484.           start with "Nik...".  Once this is done, your confirmed address will
  485.           be entered into our records.
  486.  
  487.       If you get the message back and they've followed your
  488.       directions, you can be reasonably assured that it's real.
  489.  
  490.       A related strategy that's less open to forgery is to give
  491.       them a PIN (personal ID number).  Record the address and PIN
  492.       (best    that it    be a random one) for later processing.    In the
  493.       mail you send, ask them to include the PIN in    their reply.
  494.       But if it bounces, or    the message is included    via a
  495.       ``vacation'' script, it'll be    there anyway.  So it's best to
  496.       ask them to mail back    a slight alteration of the PIN,    such
  497.       as with the characters reversed, one added or    subtracted to
  498.       each digit, etc.
  499.  
  500.       HHHHoooowwww ddddoooo IIII ddddeeeeccccooooddddeeee aaaa MMMMIIIIMMMMEEEE////BBBBAAAASSSSEEEE66664444    ssssttttrrrriiiinnnngggg????
  501.  
  502.       The MIME-tools package (available from CPAN) handles this
  503.       and a    lot more.  Decoding BASE64 becomes as simple as:
  504.  
  505.           use MIME::base64;
  506.           $decoded = decode_base64($encoded);
  507.  
  508.       A more direct    approach is to use the _u_n_p_a_c_k()    function's "u"
  509.       format after minor transliterations:
  510.  
  511.           tr#A-Za-z0-9+/##cd;            # remove non-base64    chars
  512.           tr#A-Za-z0-9+/# -_#;            # convert to uuencoded format
  513.           $len = pack("c", 32 + 0.75*length);   # compute length byte
  514.           print unpack("u",    $len . $_);        # uudecode and print
  515.  
  516.  
  517.       HHHHoooowwww ddddoooo IIII rrrreeeettttuuuurrrrnnnn tttthhhheeee uuuusssseeeerrrr''''ssss mmmmaaaaiiiillll aaaaddddddddrrrreeeessssssss????
  518.  
  519.       On systems that support getpwuid, the    $< variable and    the
  520.       Sys::Hostname    module (which is part of the standard perl
  521.       distribution), you can probably try using something like
  522.  
  523.  
  524.  
  525.      Page 8                        (printed 10/23/98)
  526.  
  527.  
  528.  
  529.  
  530.  
  531.  
  532.      PPPPEEEERRRRLLLLFFFFAAAAQQQQ9999((((1111))))     22223333////JJJJuuuullll////99998888 ((((ppppeeeerrrrllll 5555....000000005555,,,, ppppaaaattttcccchhhh 00002222))))       PPPPEEEERRRRLLLLFFFFAAAAQQQQ9999((((1111))))
  533.  
  534.  
  535.  
  536.       this:
  537.  
  538.           use Sys::Hostname;
  539.           $address = sprintf('%s@%s', getpwuid($<),    hostname);
  540.  
  541.       Company policies on mail address can mean that this
  542.       generates addresses that the company's mail system will not
  543.       accept, so you should    ask for    users' mail addresses when
  544.       this matters.     Furthermore, not all systems on which Perl
  545.       runs are so forthcoming with this information    as is Unix.
  546.  
  547.       The Mail::Util module    from CPAN (part    of the MailTools
  548.       package) provides a _m_a_i_l_a_d_d_r_e_s_s() function that tries    to
  549.       guess    the mail address of the    user.  It makes    a more
  550.       intelligent guess than the code above, using information
  551.       given    when the module    was installed, but it could still be
  552.       incorrect.  Again, the best way is often just    to ask the
  553.       user.
  554.  
  555.       HHHHoooowwww ddddoooo IIII sssseeeennnndddd    mmmmaaaaiiiillll????
  556.  
  557.       Use the sendmail program directly:
  558.  
  559.           open(SENDMAIL, "|/usr/lib/sendmail -oi -t    -odq")
  560.                   or die "Can't    fork for sendmail: $!\n";
  561.           print SENDMAIL <<"EOF";
  562.           From: User Originating Mail <me\@host>
  563.           To: Final    Destination <you\@otherhost>
  564.           Subject: A relevant subject line
  565.  
  566.           Body of the message goes here, in    as many    lines as you like.
  567.           EOF
  568.           close(SENDMAIL)      or warn "sendmail didn't close nicely";
  569.  
  570.       The ----ooooiiii option prevents sendmail from    interpreting a line
  571.       consisting of    a single dot as    "end of    message".  The ----tttt
  572.       option says to use the headers to decide who to send the
  573.       message to, and ----ooooddddqqqq says to put the message into the    queue.
  574.       This last option means your message won't be immediately
  575.       delivered, so    leave it out if    you want immediate delivery.
  576.  
  577.       Or use the CPAN module Mail::Mailer:
  578.  
  579.           use Mail::Mailer;
  580.  
  581.  
  582.  
  583.  
  584.  
  585.  
  586.  
  587.  
  588.  
  589.  
  590.  
  591.      Page 9                        (printed 10/23/98)
  592.  
  593.  
  594.  
  595.  
  596.  
  597.  
  598.      PPPPEEEERRRRLLLLFFFFAAAAQQQQ9999((((1111))))     22223333////JJJJuuuullll////99998888 ((((ppppeeeerrrrllll 5555....000000005555,,,, ppppaaaattttcccchhhh 00002222))))       PPPPEEEERRRRLLLLFFFFAAAAQQQQ9999((((1111))))
  599.  
  600.  
  601.  
  602.           $mailer =    Mail::Mailer->new();
  603.           $mailer->open({ From    => $from_address,
  604.                   To      => $to_address,
  605.                   Subject => $subject,
  606.                 })
  607.           or die "Can't    open: $!\n";
  608.           print $mailer $body;
  609.           $mailer->close();
  610.  
  611.       The Mail::Internet module uses Net::SMTP which is less
  612.       Unix-centric than Mail::Mailer, but less reliable.  Avoid
  613.       raw SMTP commands.  There are    many reasons to    use a mail
  614.       transport agent like sendmail.  These    include    queueing, MX
  615.       records, and security.
  616.  
  617.       HHHHoooowwww ddddoooo IIII rrrreeeeaaaadddd    mmmmaaaaiiiillll????
  618.  
  619.       Use the Mail::Folder module from CPAN    (part of the
  620.       MailFolder package) or the Mail::Internet module from    CPAN
  621.       (also    part of    the MailTools package).
  622.  
  623.          # sending mail
  624.           use Mail::Internet;
  625.           use Mail::Header;
  626.           #    say which mail host to use
  627.           $ENV{SMTPHOSTS} =    'mail.frii.com';
  628.           #    create headers
  629.           $header =    new Mail::Header;
  630.           $header->add('From', 'gnat@frii.com');
  631.           $header->add('Subject', 'Testing');
  632.           $header->add('To', 'gnat@frii.com');
  633.           #    create body
  634.           $body = 'This is a test, ignore';
  635.           #    create mail object
  636.           $mail = new Mail::Internet(undef,    Header => $header, Body    => \[$body]);
  637.           #    send it
  638.           $mail->smtpsend or die;
  639.  
  640.       Often    a module is overkill, though.  Here's a    mail sorter.
  641.  
  642.  
  643.  
  644.  
  645.  
  646.  
  647.  
  648.  
  649.  
  650.  
  651.  
  652.  
  653.  
  654.  
  655.  
  656.  
  657.      Page 10                        (printed 10/23/98)
  658.  
  659.  
  660.  
  661.  
  662.  
  663.  
  664.      PPPPEEEERRRRLLLLFFFFAAAAQQQQ9999((((1111))))     22223333////JJJJuuuullll////99998888 ((((ppppeeeerrrrllll 5555....000000005555,,,, ppppaaaattttcccchhhh 00002222))))       PPPPEEEERRRRLLLLFFFFAAAAQQQQ9999((((1111))))
  665.  
  666.  
  667.  
  668.           #!/usr/bin/perl
  669.           #    bysub1 - simple    sort by    subject
  670.           my(@msgs,    @sub);
  671.           my $msgno    = -1;
  672.           $/ = '';              # paragraph reads
  673.           while (<>) {
  674.           if (/^From/m)    {
  675.               /^Subject:\s*(?:Re:\s*)*(.*)/mi;
  676.               $sub[++$msgno] = lc($1) || '';
  677.           }
  678.           $msgs[$msgno]    .= $_;
  679.           }
  680.           for my $i    (sort {    $sub[$a] cmp $sub[$b] || $a <=>    $b } (0    .. $#msgs)) {
  681.           print    $msgs[$i];
  682.           }
  683.  
  684.       Or more succinctly,
  685.  
  686.           #!/usr/bin/perl -n00
  687.           #    bysub2 - awkish    sort-by-subject
  688.           BEGIN { $msgno = -1 }
  689.           $sub[++$msgno] = (/^Subject:\s*(?:Re:\s*)*(.*)/mi)[0] if /^From/m;
  690.           $msg[$msgno] .= $_;
  691.           END { print @msg[    sort { $sub[$a]    cmp $sub[$b] ||    $a <=> $b } (0 .. $#msg) ] }
  692.  
  693.  
  694.       HHHHoooowwww ddddoooo IIII ffffiiiinnnndddd    oooouuuutttt mmmmyyyy hhhhoooossssttttnnnnaaaammmmeeee////ddddoooommmmaaaaiiiinnnnnnnnaaaammmmeeee////IIIIPPPP aaaaddddddddrrrreeeessssssss????
  695.  
  696.       The normal way to find your own hostname is to call the
  697.       `hostname` program.  While sometimes expedient, this has
  698.       some problems, such as not knowing whether you've got    the
  699.       canonical name or not.  It's one of those tradeoffs of
  700.       convenience versus portability.
  701.  
  702.       The Sys::Hostname module (part of the    standard perl
  703.       distribution)    will give you the hostname after which you can
  704.       find out the IP address (assuming you    have working DNS) with
  705.       a _g_e_t_h_o_s_t_b_y_n_a_m_e() call.
  706.  
  707.           use Socket;
  708.           use Sys::Hostname;
  709.           my $host = hostname();
  710.           my $addr = inet_ntoa(scalar(gethostbyname($name))    || 'localhost');
  711.  
  712.       Probably the simplest    way to learn your DNS domain name is
  713.       to grok it out of /etc/resolv.conf, at least under Unix.  Of
  714.       course, this assumes several things about your resolv.conf
  715.       configuration, including that    it exists.
  716.  
  717.       (We still need a good    DNS domain name-learning method    for
  718.       non-Unix systems.)
  719.  
  720.  
  721.  
  722.  
  723.      Page 11                        (printed 10/23/98)
  724.  
  725.  
  726.  
  727.  
  728.  
  729.  
  730.      PPPPEEEERRRRLLLLFFFFAAAAQQQQ9999((((1111))))     22223333////JJJJuuuullll////99998888 ((((ppppeeeerrrrllll 5555....000000005555,,,, ppppaaaattttcccchhhh 00002222))))       PPPPEEEERRRRLLLLFFFFAAAAQQQQ9999((((1111))))
  731.  
  732.  
  733.  
  734.       HHHHoooowwww ddddoooo IIII ffffeeeettttcccchhhh aaaa nnnneeeewwwwssss    aaaarrrrttttiiiicccclllleeee    oooorrrr tttthhhheeee aaaaccccttttiiiivvvveeee nnnneeeewwwwssssggggrrrroooouuuuppppssss????
  735.  
  736.       Use the Net::NNTP or News::NNTPClient    modules, both
  737.       available from CPAN.    This can make tasks like fetching the
  738.       newsgroup list as simple as:
  739.  
  740.           perl -MNews::NNTPClient
  741.         -e 'print News::NNTPClient->new->list("newsgroups")'
  742.  
  743.  
  744.       HHHHoooowwww ddddoooo IIII ffffeeeettttcccchhhh////ppppuuuutttt aaaannnn    FFFFTTTTPPPP ffffiiiilllleeee????
  745.  
  746.       LWP::Simple (available from CPAN) can    fetch but not put.
  747.       Net::FTP (also available from    CPAN) is more complex but can
  748.       put as well as fetch.
  749.  
  750.       HHHHoooowwww ccccaaaannnn IIII ddddoooo RRRRPPPPCCCC iiiinnnn PPPPeeeerrrrllll????
  751.  
  752.       A DCE::RPC module is being developed (but is not yet
  753.       available), and will be released as part of the DCE-Perl
  754.       package (available from CPAN).  No ONC::RPC module is    known.
  755.  
  756.      AAAAUUUUTTTTHHHHOOOORRRR AAAANNNNDDDD    CCCCOOOOPPPPYYYYRRRRIIIIGGGGHHHHTTTT
  757.       Copyright (c)    1997, 1998 Tom Christiansen and    Nathan
  758.       Torkington.  All rights reserved.
  759.  
  760.       When included    as part    of the Standard    Version    of Perl, or as
  761.       part of its complete documentation whether printed or
  762.       otherwise, this work may be distributed only under the terms
  763.       of Perl's Artistic License.  Any distribution    of this    file
  764.       or derivatives thereof _o_u_t_s_i_d_e of that package require that
  765.       special arrangements be made with copyright holder.
  766.  
  767.       Irrespective of its distribution, all    code examples in this
  768.       file are hereby placed into the public domain.  You are
  769.       permitted and    encouraged to use this code in your own
  770.       programs for fun or for profit as you    see fit.  A simple
  771.       comment in the code giving credit would be courteous but is
  772.       not required.
  773.  
  774.  
  775.  
  776.  
  777.  
  778.  
  779.  
  780.  
  781.  
  782.  
  783.  
  784.  
  785.  
  786.  
  787.  
  788.  
  789.      Page 12                        (printed 10/23/98)
  790.  
  791.  
  792.  
  793.  
  794.  
  795.  
  796.      PPPPEEEERRRRLLLLFFFFAAAAQQQQ9999((((1111))))     22223333////JJJJuuuullll////99998888 ((((ppppeeeerrrrllll 5555....000000005555,,,, ppppaaaattttcccchhhh 00002222))))       PPPPEEEERRRRLLLLFFFFAAAAQQQQ9999((((1111))))
  797.  
  798.  
  799.  
  800.  
  801.  
  802.  
  803.  
  804.  
  805.  
  806.  
  807.  
  808.  
  809.  
  810.  
  811.  
  812.  
  813.  
  814.  
  815.  
  816.  
  817.  
  818.  
  819.  
  820.  
  821.  
  822.  
  823.  
  824.  
  825.  
  826.  
  827.  
  828.  
  829.  
  830.  
  831.  
  832.  
  833.  
  834.  
  835.  
  836.  
  837.  
  838.  
  839.  
  840.  
  841.  
  842.  
  843.  
  844.  
  845.  
  846.  
  847.  
  848.  
  849.  
  850.  
  851.  
  852.      Page 13                        (printed 10/23/98)
  853.  
  854.  
  855.  
  856.  
  857.  
  858.  
  859.